export function zeroize(intNumber, fixed) {
    var number = parseInt(intNumber);
    var fixedI = parseInt(fixed);
    if(isNaN(number) || isNaN(fixedI)) {
        return NaN;
    }
    var fixedNum = Math.pow(10, fixedI);
    var zeroizeNumber = fixedNum + number;
    var zeroizeNumberStr = zeroizeNumber.toString().slice(-fixedI);
    return zeroizeNumberStr;
}

function formatTimestamp (timestamp, formatString){
    //获得时间
    var date = new Date(timestamp);
    //解析
    var tempTime = {
        year: date.getFullYear(),
        month: date.getMonth() + 1,
        monthZ: zeroize(date.getMonth() + 1, 2),
        day: date.getDate(),
        dayZ: zeroize(date.getDate(), 2),
        hours: date.getHours(),
        hoursZ: zeroize(date.getHours(), 2),
        hours12: date.getHours()%12,
        hours12Z: zeroize(date.getHours()%12, 2),
        amOrPm: parseInt(date.getHours()/12) == 0 ? "AM" : "PM",
        min: date.getMinutes(),
        minZ: zeroize(date.getMinutes(), 2),
        sec: date.getSeconds(),
        secZ: zeroize(date.getSeconds(), 2)
    };
    var timeString = formatString;
    timeString = timeString.replace(/yyyy/g, tempTime.year);
    timeString = timeString.replace(/MM/g, tempTime.monthZ);
    timeString = timeString.replace(/M/g, tempTime.month);
    timeString = timeString.replace(/dd/g, tempTime.dayZ);
    timeString = timeString.replace(/d/g, tempTime.day);
    timeString = timeString.replace(/HH/g, tempTime.hoursZ);
    timeString = timeString.replace(/H/g, tempTime.hours);
    timeString = timeString.replace(/hh/g, tempTime.hours12Z);
    timeString = timeString.replace(/h/g, tempTime.hours12);
    timeString = timeString.replace(/mm/g, tempTime.minZ);
    timeString = timeString.replace(/m/g, tempTime.min);
    timeString = timeString.replace(/ss/g, tempTime.secZ);
    timeString = timeString.replace(/s/g, tempTime.sec);
    timeString = timeString.replace(/a/g, tempTime.amOrPm);
    return timeString;
}

function formatFee(money, fixed) {
    var moneyF = parseFloat(money);
    var fixedI = parseInt(fixed);
    if(isNaN(moneyF) || isNaN(fixedI)) {
        return NaN;
    }
    return maneyF.toFixed(fixedI);
}

function addFee(fees, fixed) {
    var fixedI = parseInt(fixed);
    if(isNaN(fixedI)) {
        return NaN;
    }
    var fixNum = Math.pow(10, fixed);
    var sum = 0;
    for(var fee of fees) {
        var feeF = parseFloat(fee);
        if(isNaN(moneyF)) {
            return NaN;
        }
        sum += parseInt(feeF * fixNum);
    }
    return sum/fixNum; 
}

function timeZeroize(time) {
    let hour = time.split(':')[0];
    let minute = time.split(':')[1];
    if(parseInt(hour) < 10){
      hour = '0' + hour;
    }
    if(parseInt(minute) < 10){
      minute = '0' + minute;
    }
    return hour + ':' + minute;
}

function timeZeroSuppresse(time) {
    let hour = time.split(':')[0];
    let minute = time.split(':')[1];
    hour = parseInt(hour);
    minute = parseInt(minute);
    return hour + ':' + minute;
}